package com.meihua.shellapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.meihua.shellapp.R;

/**
 * 作者：lc on 2017/9/28.
 * 邮箱：lc0607live@163.com
 */

public class BaseMessageDialog extends Dialog {

    private TextView tv_sure;//确定
    private TextView tv_message;//消息
    private TextView tv_cencle;//取消

    private View.OnClickListener mClickSure;
    private View.OnClickListener mClickCencle;
    public String mStrMessage;
    public int cencleShow;
    public boolean cancel = true;
    public boolean canceltouch = true;

    private BaseMessageDialog(@NonNull Context context, View.OnClickListener clickSure,
                              String message, int cencleShow, View.OnClickListener mClickCencle, boolean cancel, boolean canceltouch) {
        super(context, R.style.MyDialog);
        this.mClickCencle = mClickCencle != null ? mClickCencle : new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        };
        this.mClickSure = clickSure;
        this.mStrMessage = message;
        this.cencleShow = cencleShow;
        this.cancel = cancel;
        this.canceltouch = canceltouch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_base_message);
        setCanceledOnTouchOutside(false);
        setCancelable(canceltouch);
        initView();
    }

    private void initView() {
        tv_sure = findViewById(R.id.tv_sure);
        tv_message = findViewById(R.id.tv_message);
        tv_cencle = findViewById(R.id.tv_cencle);
        tv_cencle.setVisibility(cencleShow);
        tv_message.setText(mStrMessage);
        tv_sure.setOnClickListener(mClickSure);
        tv_cencle.setOnClickListener(mClickCencle);
    }


    public BaseMessageDialog setSureStr(String sure) {
        tv_sure.setText(sure);
        return this;
    }

    public BaseMessageDialog setCencleStr(String cencle) {
        tv_cencle.setText(cencle);
        return this;
    }


    public static class Builder {

        Context mContext;
        View.OnClickListener mClickSure;
        String mStrMessage;
        int mCencleShow;
        View.OnClickListener mClickCencle;
        boolean cancel = true;
        boolean canceltouch = true;


        public Builder(Context context) {
            this.mContext = context;
        }

        public BaseMessageDialog create() {
            final BaseMessageDialog dialog = new BaseMessageDialog(mContext, mClickSure, mStrMessage, mCencleShow, mClickCencle, cancel, canceltouch);
            return dialog;
        }

        public Builder setMessage(String message) {
            mStrMessage = message;
            return this;
        }

        public Builder isCancel(boolean cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder isCancelTouch(boolean canceltouch) {
            this.canceltouch = canceltouch;
            return this;
        }

        public Builder setClickSure(View.OnClickListener sure) {
            mClickSure = sure;
            return this;
        }

        public Builder setClickCencle(View.OnClickListener sure) {
            mClickCencle = sure;
            return this;
        }

        public Builder setCencleShow(int show) {
            mCencleShow = show;
            return this;
        }

        public BaseMessageDialog show() {
            BaseMessageDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }

}
